//
//  DissertationViewController.m
//  chanyou
//
//  Created by wanli on 15/1/2.
//  Copyright (c) 2015年 wanli. All rights reserved.
// ------------------ 专题列表 ------------------------

#import "DissertationViewController.h"
//#import "SaveAndReadFile.h"
#import "UIImageView+WebCache.h"
#import "ConnectModel.h"
#import "DissertationTableViewCell.h"
#import "ArtDetailViewController.h"
#import "SDImageCache.h"
@interface DissertationViewController ()

@end

@implementation DissertationViewController
- (void)dealloc
{
    [_destination_id release];
    [_tableArray release];
    [_tableView release];
    [_reloadImg release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSMutableArray array];
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    [self creatTableView];
    [self startConnect];
    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"iconfont-back.png"] style:UIBarButtonItemStyleDone target:self action:@selector(backAction)]autorelease];
    // navigation的title
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 0, 45, 100)];
    label.textColor = [UIColor colorWithRed:255 / 255.0 green:69 / 255.0 blue:0 alpha:1];
    label.font = [UIFont fontWithName:@"TimesNewRomanPS-BoldMT" size:20];
    label.text = @"专题";
    self.navigationItem.titleView = label;

    // Do any additional setup after loading the view.
}

- (void)creatTableView{
    self.tableView = [[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain] autorelease];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerClass:[DissertationTableViewCell class] forCellReuseIdentifier:@"disCell"];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];

    
    // 风火轮
    self.reloadImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"reload.png"]];
    _reloadImg.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height/2 - 64);
    // _reloadImg.clipsToBounds = YES;
    [self.view addSubview:_reloadImg];
    [_reloadImg release];
    
    CABasicAnimation *rotationAnimation;
    rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.toValue = [NSNumber numberWithFloat:M_PI * 2.0];
    [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    rotationAnimation.duration = 2;
    rotationAnimation.repeatCount = 10000;
    rotationAnimation.cumulative = NO;
    rotationAnimation.removedOnCompletion = NO;
    rotationAnimation.fillMode = kCAFillModeForwards;
    [_reloadImg.layer addAnimation:rotationAnimation forKey:@"rotation"];
}

- (void)backAction
{
    [self.navigationController popViewControllerAnimated:YES];
    
    //    [[SDImageCache sharedImageCache] clearMemory];
}





- (void)startConnect{
    __block DissertationViewController *diss = self;
    NSString *path = [NSString stringWithFormat:@"%@%@&page=1", @"http://chanyouji.com/api/articles.json?destination_id=", _destination_id];
    [ConnectModel connectModelWithUrl:path parmaters:nil finished:^(NSData *data) {
        
        [_reloadImg removeFromSuperview];
        
        diss.tableArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        [diss.tableView reloadData];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [_tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DissertationTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"disCell"];
    NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    NSString *imagePath = [dic objectForKey:@"image_url"];
//    [cell.image_urlView imageWithUrl:imagePath placeHolder:nil];
    [cell.image_urlView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:[UIImage imageNamed: @"placeHold.png"]];
    
    cell.name.text = [dic objectForKey:@"name"];
    cell.title.text = [dic objectForKey:@"title"];

    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ArtDetailViewController *trip = [[ArtDetailViewController alloc] init];
     NSDictionary *dic = [_tableArray objectAtIndex:indexPath.row];
    NSInteger num = [[dic objectForKey:@"id"] intValue];
    trip.detailId = num;
    [self.navigationController pushViewController:trip animated:YES];
        [[SDImageCache sharedImageCache] clearMemory];
    [trip release];
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.view.frame.size.height / 2.5 - 15;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
